package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivityVideoManageBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class VideoManageActivity extends BaseAc<ActivityVideoManageBinding> {

    private SelVideoAdapter videoAdapter;
    private List<SelectMediaEntity> listDelete = new ArrayList<>();
    private boolean isEdit = false;
    private boolean isSelectAll = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_video_manage;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            isEdit = false;
            listDelete.clear();
            videoAdapter.setEdit(isEdit);
            getData();
            mDataBinding.ivVideoManageEdit.setVisibility(View.VISIBLE);
            mDataBinding.llVideoManageBottom.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivVideoManageBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new SelVideoAdapter();
        videoAdapter.setEdit(isEdit);
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);

        mDataBinding.ivVideoManageEdit.setOnClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivVideoManageEdit://管理
                isEdit = true;
                videoAdapter.setEdit(isEdit);
                videoAdapter.notifyDataSetChanged();
                mDataBinding.ivVideoManageEdit.setVisibility(View.GONE);
                mDataBinding.llVideoManageBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.llSelectAll://全选
                listDelete.clear();
                if (isSelectAll) {
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        validDatum.setChecked(true);
                        listDelete.add(validDatum);
                    }
                }
                isSelectAll();
                videoAdapter.notifyDataSetChanged();
                break;
            case R.id.tvDelete://删除
                if (listDelete.size() == 0) {
                    return;
                }
                delete();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (videoAdapter.getItem(position).isChecked()) {
                videoAdapter.getItem(position).setChecked(false);
                listDelete.remove(videoAdapter.getItem(position));
                videoAdapter.notifyDataSetChanged();
            } else {
                listDelete.add(videoAdapter.getItem(position));
                videoAdapter.getItem(position).setChecked(true);
                videoAdapter.notifyDataSetChanged();
            }
            isSelectAll();
        } else {
            VideoFileDetailActivity.videoPath = videoAdapter.getItem(position).getPath();
            startActivity(VideoFileDetailActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_video_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvVideoList.setVisibility(View.GONE);
                        mDataBinding.ivVideoManageNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.ivVideoManageNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.ivVideoManageNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        if (listDelete.size() == videoAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanxuan);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aweiquanx);
            isSelectAll = false;
        }
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (int i = 0; i < listDelete.size(); i++) {
                            //从此文件夹删除
                            mContext.getContentResolver().delete(Uri.parse(listDelete.get(i).getUri()), null, null);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        listDelete.clear();
                        isEdit = false;
                        videoAdapter.setEdit(isEdit);
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        mDataBinding.llVideoManageBottom.setVisibility(View.GONE);
                    }
                });
            }
        }, 1000);
    }

}